﻿using ScanConfirm.Business;
using ScanConfirm.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace ScanConfirmForm
{
    public partial class FrmPartsEquateSet : Form
    {
        PartsSetupBLL partsSetupBLL = new PartsSetupBLL();
        PartsCodeEquateBLL partsCodeEquateBLL = new PartsCodeEquateBLL();
        PartsCodeEquateModel partsCodeEquateModel;
        public Action RefreshUiEvent;
        public FrmPartsEquateSet()
        {
            InitializeComponent();
        }

        public FrmPartsEquateSet(PartsCodeEquateModel model)
        {
            InitializeComponent();
            partsCodeEquateModel = model;
            cbPartCode.Text = model.PartsCode;
            txtPartCodeEquate.Text = model.PartsCodeEquate;
            btnSave.Text = "修改";
        }

        private void FrmPartsEquateSet_Load(object sender, EventArgs e)
        {
            var list = partsSetupBLL.GetModelAll();
            cbPartCode.DataSource = list;
            cbPartCode.DisplayMember = "DisplayCode";
            cbPartCode.ValueMember = "PartsCode";
            cbPartCode.SelectedIndex = 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (partsCodeEquateModel == null)
            {
                var model = partsCodeEquateBLL.GetPartsSetupByCode(cbPartCode.ValueMember, txtPartCodeEquate.Text);
                if (model != null)
                {
                    MessageBox.Show("该配置已存在，不能重复！");
                    return;
                }
                partsCodeEquateModel = new PartsCodeEquateModel();
                partsCodeEquateModel.PartsCode = cbPartCode.SelectedValue.ToString();
                partsCodeEquateModel.DisplayCode = cbPartCode.Text;
                partsCodeEquateModel.PartsCodeEquate = txtPartCodeEquate.Text;


                var result = partsCodeEquateBLL.Add(partsCodeEquateModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("添加品番等同成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("添加品番等同失败：" + result.Dsca);
            }
            else
            {
                partsCodeEquateModel.PartsCode = cbPartCode.SelectedValue.ToString();
                partsCodeEquateModel.DisplayCode = cbPartCode.Text;
                partsCodeEquateModel.PartsCodeEquate = txtPartCodeEquate.Text;
                var result = partsCodeEquateBLL.Update(partsCodeEquateModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("修改品番等同成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("修改品番等同失败：" + result.Dsca);
            }
        }

    }
}
